home *** CD-ROM | disk | FTP | other *** search
/ Turnbull China Bikeride / Turnbull China Bikeride - Disc 2.iso / STUTTGART / UTIL / MEMORY / OLD / MEM208SRC / FSLib / c / filer < prev    next >
Text File  |  1993-08-22  |  10KB  |  395 lines

  1. /* Original code (c) Acorn Computers Ltd, 1992-3 */
  2.  
  3. /* $Id: c.filer 3.1 93/03/09 20:00:45 brian Exp $ */
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <string.h>
  7. #include "kernel.h"
  8. #include "swis.h"
  9. #include "ModuleWrap.h"
  10. #include "util.h"
  11. #include "cstart.h"
  12. #include "wimp.h"
  13.  
  14. #define RUNNABLE        /* to enable direct running */
  15.  
  16. #define TASK    ('K'<<24|'S'<<16|'A'<<8|'T')
  17.  
  18. int             taskhandle;
  19. int             taskstack;
  20.  
  21. static int      filertaskhandle;
  22. static char FilerName[]= FS"FSFiler";
  23.  
  24. /* Define TRACESERVICE in order to output trace of service calls */
  25. /* #define TRACESERVICE */
  26.  
  27. static void removetask(void)
  28. {
  29.         _kernel_swi_regs r;
  30.  
  31.         r.r[0]=taskhandle;
  32.         r.r[1]=TASK;
  33.         taskhandle=0;
  34.         if (r.r[0] && r.r[0]!=-1)
  35.           _kernel_swi( XOS_Bit | Wimp_CloseDown , &r, &r );
  36.         if (taskstack)
  37.           free((char *)taskstack);
  38.         taskstack=0;
  39. }
  40.  
  41. /*
  42.         This is the finalisation code
  43. */
  44.  
  45. static void fs_finalise
  46. ( void )
  47. { removetask();
  48. }
  49.  
  50. /*
  51.         This is the initialisation code
  52. */
  53. _kernel_oserror *fs_initialise
  54. (
  55.         char *cmd_tail,
  56.         int podule_base,
  57.         void *private_word
  58. )
  59. {
  60.         _kernel_oserror *err=NULL;
  61.  
  62.         private_word=private_word;
  63.         /*
  64.                 These keep the compiler quiet.
  65.         */
  66.         cmd_tail = cmd_tail;
  67.         podule_base = podule_base;
  68.  
  69.         /*
  70.                 Must record fact filer task is not running
  71.         */
  72.         taskhandle = 0;
  73.         taskstack = 0 ; /* and that its stack is not yet allocated */
  74.  
  75.         atexit( fs_finalise );
  76.  
  77.         return err;
  78. }
  79.  
  80. /*
  81.         Service call handler
  82. */
  83.  
  84. #ifdef TRACESERVICE
  85. static struct {int svc; char *name;} svcs[] =
  86. {
  87.  {0x00,"Serviced"},
  88.  {0x04,"UKCommand"},
  89.  {0x06,"Error"},
  90.  {0x07,"UKByte"},
  91.  {0x08,"UKWord"},
  92.  {0x09,"Help"},
  93.  {0x0b,"ReleaseFIQ"},
  94.  {0x0c,"ClaimFIQ"},
  95.  {0x11,"Memory"},
  96.  {0x12,"StartUpFS"},
  97.  {0x27,"Reset"},
  98.  {0x28,"UKConfig"},
  99.  {0x29,"UKStatus"},
  100.  {0x2a,"NewApplication"},
  101.  {0x40,"FSRedclare"},
  102.  {0x41,"Print"},
  103.  {0x42,"LookupFileType"},
  104.  {0x43,"International"},
  105.  {0x44,"Keyhandler"},
  106.  {0x45,"PreReset"},
  107.  {0x46,"ModeChange"},
  108.  {0x47,"ClaimFIQinBackground"},
  109.  {0x48,"ReAllocatePorts"},
  110.  {0x49,"StartWimp"},
  111.  {0x4a,"StartedWimp"},
  112.  {0x4b,"StartFiler"},
  113.  {0x4c,"StartedFiler"},
  114.  {0x4d,"PreModeChange"},
  115.  {0x4e,"MemoryMoved"},
  116.  {0x4f,"FilerDying"},
  117.  {0x50,"ModeExtension"},
  118.  {0x51,"ModeTranslation"},
  119.  {0x52,"MouseTrap"},
  120.  {0x53,"WimpCloseDown"},
  121.  {0x54,"Sound"},
  122.  {0x55,"NetFS"},
  123.  {0x56,"EconetDying"},
  124.  {0x57,"WimpReportError"},
  125.  {-1,"Something else"}
  126. };
  127. #endif
  128.  
  129. void fs_service
  130. (
  131.         int service_number,
  132.         _kernel_swi_regs *r,
  133.         void *private_word
  134. )
  135. {
  136. #ifdef TRACESERVICE
  137. { int i;
  138.  for (i=0;svcs[i].svc>=0;i++) if (svcs[i].svc==service_number)break;
  139.  printf("Service_%s (%#x)%x,%x,%x,%x,%x,%x,%x,%x,%x,%x\r\n",svcs[i].name,service_number,*r);
  140. }
  141. #endif
  142.         private_word=private_word;
  143.         switch( service_number )
  144.         {
  145.         case Service_StartFiler:
  146.           if (!taskhandle)
  147.           {
  148.             filertaskhandle=r->r[0];
  149.             taskhandle=-1; /* attempting start flag */
  150.             r->r[0]=(int)("Desktop_"FS"FSFiler");
  151.             r->r[1]=0;
  152.           }
  153.           break;
  154.         case Service_StartedFiler:
  155.           if (taskhandle==-1)
  156.             taskhandle=0;
  157.           break;
  158.         case Service_Reset:
  159.           taskhandle=0;
  160.           break;
  161.         case Service_FilerDying:
  162.           removetask();
  163.           break;
  164.  
  165.         /*
  166.                 Handle other service calls here
  167.         */
  168.         }
  169. }
  170.  
  171. /*
  172.         command processor
  173. */
  174. _kernel_oserror *fs_command
  175. (
  176.         char *arg_string,
  177.         int argc,
  178.         int cmd_no,
  179.         void *private_word
  180. )
  181. {
  182.         _kernel_swi_regs r;
  183. #define My_Command_Parameter_Buffer_Len 256
  184. static  char my_command_parameter_buffer[ My_Command_Parameter_Buffer_Len ];
  185.         char *temptr = my_command_parameter_buffer;
  186.  
  187.         argc = argc;
  188.         private_word = private_word;
  189.  
  190.         /*
  191.                 Change the control character terminator to a nul terminator
  192.         */
  193.         for ( ; temptr - my_command_parameter_buffer < My_Command_Parameter_Buffer_Len && *arg_string >= ' ' ; )
  194.         {
  195.                 *temptr++ = *arg_string++;
  196.         }
  197.  
  198.         *temptr = '\0';
  199.  
  200.         arg_string = my_command_parameter_buffer;
  201.  
  202.         /*
  203.                 Switch between the commands
  204.         */
  205.         switch ( cmd_no )
  206.         {
  207.         case 0: /*      *Desktop_FSnameFSFiler */
  208.                 if (taskhandle!=-1)
  209.                 {
  210. #ifndef RUNNABLE
  211.                   printf("Use *Desktop to start %s\n",FilerName);
  212.                   return NULL;
  213. #else
  214.                   removetask();
  215.                   taskhandle=-1;
  216. #endif
  217.                 }
  218.                 r.r[0] = OSModule_Enter;
  219.                 r.r[1] = (int)FilerName;
  220.                 r.r[2] = (int)arg_string;
  221.                 return _kernel_swi( XOS_Bit | OS_Module, &r, &r );
  222.         default:
  223.                 return NULL;
  224.         }
  225. }
  226.  
  227. int filertask( char *cmdtail );
  228.  
  229. #define MENUDEPTH 2
  230.  
  231. static struct
  232. { wimp_menuhdr hdr;
  233.   wimp_menuitem item[MENUDEPTH];
  234. } mymenu=
  235. { { "Menutitle", 7, 2, 7, 0, 96, 44, 0 },
  236.   { {
  237.       0
  238.         , (wimp_menuptr)-1, wimp_ITEXT |
  239.                          wimp_IBTYPE*wimp_BNOTIFY |
  240.                          wimp_IFORECOL*7 |
  241.                          wimp_IBACKCOL*0, "Free" },
  242.     { wimp_MLAST
  243.         , (wimp_menuptr)-1, wimp_ITEXT |
  244.                          wimp_IBTYPE*wimp_BNOTIFY |
  245.                          wimp_IFORECOL*7 |
  246.                          wimp_IBACKCOL*0, "Quit" }
  247. } } ;
  248.  
  249. int filertask
  250. ( char *cmdtail )
  251. {
  252.   _kernel_swi_regs r;
  253.   os_error *err;
  254.   wimp_icreate wi;
  255.   wimp_i icon;
  256.   wimp_eventstr ev;
  257.   int wimpversion;
  258.   int maydataload=0;
  259. #ifndef RUNNABLE
  260.   if (taskhandle!=-1)
  261.     return 0;
  262. #endif
  263.   cmdtail=cmdtail; /* Use it to scare off warnings */
  264.   r.r[0]=200;
  265.   r.r[1]=TASK;
  266.   r.r[2]=(int)FilerName;
  267.   err=(os_error *)_kernel_swi( Wimp_Initialise, &r, &r );
  268. /*  err=wimp_taskinit( FilerName, (wimp_t*)&taskhandle ); */
  269.   if (err)
  270.     goto error;
  271.   wimpversion=r.r[0];
  272.   taskhandle=r.r[1];
  273.   if (wimpversion>200)
  274.     wi.w=-6, r.r[0]=0x40000000;
  275.   else
  276.     wi.w=-2;
  277.   wi.i.box.x0=0;
  278.   wi.i.box.y0=-16; /* base of text, 32 high */
  279.   wi.i.box.x1=2*sprx;
  280.   wi.i.box.y1=-16+32+4+4*spry;
  281.   wi.i.flags=wimp_ITEXT |
  282.              wimp_ISPRITE |
  283.              wimp_IHCENTRE |
  284.              wimp_INDIRECT |
  285.              wimp_IBTYPE*wimp_BCLICKDEBOUNCE |
  286.              wimp_IFORECOL*7 |
  287.              wimp_IBACKCOL*1 ;
  288.   wi.i.data.indirecttext.buffer=FS;
  289.   wi.i.data.indirecttext.validstring=validstr;
  290.   wi.i.data.indirecttext.bufflen=3;
  291.   r.r[1]=(int)&wi;
  292.   err=(os_error *)_kernel_swi( Wimp_CreateIcon, &r, &r );
  293.   icon=r.r[0];
  294.   /* err=wimp_create_icon(&wi,&icon); */
  295.   if (err)
  296.     goto error;
  297.   for (;;)
  298.   {
  299.     maydataload = 0;
  300. pol:err=wimp_poll(wimp_EMNULL,&ev);
  301.     if (err)
  302.       goto error;
  303.     switch (ev.e)
  304.     {
  305.     case wimp_EBUT:
  306.       switch (ev.data.but.m.bbits)
  307.       {
  308.       case wimp_BLEFT:
  309.       case wimp_BRIGHT:
  310.         ev.data.msg.hdr.size=sizeof(wimp_msghdr)+sizeof(int)*2+(strlen(RootName)&~3)+4;
  311.         ev.data.msg.hdr.your_ref=0;
  312.         ev.data.msg.hdr.action=wimp_FilerOpenDir;
  313.         ev.data.msg.data.words[0]=FilingSystemNumber;
  314.         ev.data.msg.data.words[1]=0;
  315.         strcpy((char *)&ev.data.msg.data.words[2],
  316.                                 ev.data.but.m.bbits==wimp_BLEFT?RootName:RootName1 );
  317.         err=wimp_sendmessage(wimp_ESEND,&ev.data.msg,(wimp_t)filertaskhandle);
  318.         if (err)
  319.           goto error;
  320.         continue;
  321.       case wimp_BMID:
  322.         strcpy(mymenu.hdr.title,FS"FS");
  323.         err=wimp_create_menu((wimp_menustr *)&mymenu,ev.data.but.m.x-64,96+MENUDEPTH*44);
  324.         if (err)
  325.           goto error;
  326.         continue;
  327.       }
  328.       break;
  329.     case wimp_EMENU:
  330.       switch (ev.data.menu[0])
  331.       {
  332.       case 0: /* Free */
  333.         if (_kernel_oscli("ShowFree -fs "FS" "DISK)==_kernel_ERROR)
  334.           _kernel_oscli("WimpTask "FS":Free "DISK);
  335.         continue;
  336.       case 1: /* Quit */
  337.         r.r[0]=17;
  338.         r.r[1]=(int)&RootName;
  339.         if (quitmsg[0] && !_kernel_swi( OS_File, &r, &r ) && r.r[4] )
  340.         { r.r[0]=(int)&quitmsg;
  341.           r.r[1]=3+(1<<4);
  342.           r.r[2]=(int)FilerName;
  343.           err=(os_error *)_kernel_swi( Wimp_ReportError, &r, &r );
  344.           if (r.r[1]!=1)
  345.             continue;
  346.         }
  347.         _kernel_oscli("RMKill "FS"FS");
  348. #ifdef KILLCMD1
  349.         _kernel_oscli(KILLCMD1);
  350. #endif
  351.         r.r[0]=3;
  352.         r.r[1]=-4<<20;
  353.         _kernel_swi( OS_ChangeDynamicArea, &r, &r );
  354.         r.r[0]=0;
  355.         r.r[3]=(int)&FilerName;
  356.         _kernel_swi( OS_ExitAndDie, &r, &r );
  357.       }
  358.       break;
  359.     case wimp_ESEND: case wimp_ESENDWANTACK:
  360.       switch (ev.data.msg.hdr.action)
  361.       { char buf[256];
  362.       case wimp_MCLOSEDOWN:
  363.         break;
  364.       case wimp_MDATASAVE:
  365.         strcpy(buf,RootName); strcat(buf,"."); strcat(buf,ev.data.msg.data.datasaveok.name);
  366.         strcpy(ev.data.msg.data.datasaveok.name,buf);
  367.         ev.data.msg.hdr.size = (strlen(ev.data.msg.data.datasaveok.name)|3)+1+44;
  368.         ev.data.msg.hdr.your_ref = ev.data.msg.hdr.my_ref;
  369.         ev.data.msg.hdr.action = wimp_MDATASAVEOK;
  370.         wimp_sendmessage(wimp_ESEND,&ev.data.msg,ev.data.msg.hdr.task);
  371.         maydataload = 1;
  372.         goto pol;
  373.       case wimp_MDATALOAD:
  374.         if (maydataload)
  375.         { ev.data.msg.hdr.your_ref = ev.data.msg.hdr.my_ref;
  376.           ev.data.msg.hdr.action = wimp_MDATALOADOK;
  377.           wimp_sendmessage(wimp_ESEND,&ev.data.msg,ev.data.msg.hdr.task);
  378.           maydataload = 0;
  379.         }
  380.         continue;
  381.       default:
  382.         continue;
  383.       }
  384.       break;
  385.     default:
  386.       continue;
  387.     }
  388.     break;
  389.   }
  390.   return 0;
  391. error:
  392.   wimp_reporterror(err,0, FilerName);
  393.   return (int)err;
  394. }
  395.